package br.great.DAO;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import br.great.beans.TrackPoint;

/**
* PhotoMap
* Copyright (c) 2007-2008 STEAMER - LIG, All rights reserved.
*/

/**
 * @author Developpement of PhotoMap
 */
public class DAO_Trackpoint {

    private static volatile DAO_Trackpoint instaceDAO_Trackpoint = new DAO_Trackpoint();
    
    /** The private constructor of DAO_Collection for singleton
    */
    private DAO_Trackpoint(){
    
    }
    
    /** The method for get the instance of the class DAO_Collection
    * @return DAO_Collection instaceDAO_Collection
    */
    public static DAO_Trackpoint getInstance() {
        //if(instaceDAO_Collection==null)
           synchronized(DAO_Trackpoint.class) {
             if (instaceDAO_Trackpoint == null)
               instaceDAO_Trackpoint = new DAO_Trackpoint();
           }
        return instaceDAO_Trackpoint;
    }
    
    public void insertTrackpointVector(int idCollection, Vector<TrackPoint> Vtrackpoint){
        for (int i=0; i<Vtrackpoint.size();i++){
            try {
                TrackPoint trackpoint = Vtrackpoint.elementAt(i);
                String insertInTrackpoint = "insert into trackpoint values(nextval('seqmax_trackpoint'), " + idCollection + ",'" + trackpoint.getInstant() + "', '" + trackpoint.getTrackPointCoordinateAsString() + "');";

                PgConnect conn = PgConnect.getInstance();
                conn.executeInsert(insertInTrackpoint);
                conn.Close();
            } catch (SQLException ex) {
                Logger.getLogger(DAO_Trackpoint.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public void insertTrackpoint(int idCollection, TrackPoint trackpoint){
        try {
            
            String insertInTrackpoint = "insert into trackpoint values(nextval('seqmax_trackpoint'), " + idCollection + ", '" + trackpoint.getInstant() + "', '" + trackpoint.getTrackPointCoordinateAsString() + "');";

//        System.out.println(insertInTrackpoint);
            PgConnect conn = PgConnect.getInstance();
            conn.executeInsert(insertInTrackpoint);
            conn.Close();
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Trackpoint.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
//    public static void main(String[] args){
//        DAO_Trackpoint dt = DAO_Trackpoint.getInstance();
//        Vector vTrackPointList = new Vector();
////        TrackPoint tp = new TrackPoint();
////        tp.setInstant(Timestamp.valueOf("1999-01-11 20:38:40"));
////        tp.setLatitude(45.19229038053758);
////        tp.setLongitude(5.767221450805664);
////        setTrackPointCoordinateAsString("POINT(45.19229038053758 5.767221450805664)");
////        dt.insertTrackpoint(1, tp);
//        for (int k=0;k<5;k++){
//            TrackPoint tp = new TrackPoint();
//            tp.setInstant(Timestamp.valueOf("1999-01-09 12:23:2"+k));
//            tp.setLatitude(k);
//            tp.setLongitude(k);
//            vTrackPointList.add(tp);  
//        }
//        dt.insertTrackpointVector(1, vTrackPointList);
//    }
    
}
